package zero.tech.wsnet.controller;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CacheOnline {
	private final Map<Integer, Controller> onlines = new ConcurrentHashMap<Integer, Controller>();
	private static CacheOnline cache = new CacheOnline();

	private CacheOnline() {
	}

	public static CacheOnline getCache() {
		return cache;
	}

	public Map<Integer, Controller> getOnlines() {
		return onlines;
	}

	public void online(Controller controller) {
		int playerId = controller.getId();
		if (onlines.containsKey(playerId)) {
			Controller old = onlines.get(playerId);
			if (old.getWebClient().getSession() != null
					&& !old.getWebClient().getSessionId().equals(controller.getWebClient().getSessionId())) {
				old.getWebClient().activeClose();
			}
		}
		onlines.put(controller.getId(), controller);
	}

	public void offline(Controller controller) {
		if(onlines.containsKey(controller.getId())) {
			onlines.remove(controller.getId());
		}
	}
}
